package com.example.service.impl;
import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : chenjianhua
 * @date : 17:03 2021/2/23
 */
@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserDao userDao;

    //注册
    @Override
    public void regist(User user) throws Exception{
        try {
            User user1 = userDao.findByName(user);
            if (user1 != null){
                throw new Exception("账号已经被注册");
            }
            userDao.insert(user);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    //登陆
    @Override
    public User login(User user) throws Exception{
        User res = userDao.findByName(user);
        if (res==null){
            throw new Exception("用户不存在");
        }
        if(!res.getPassword().equals(user.getPassword())){
            throw new Exception("密码错误");
        }
        return res;
    }


    @Override
    public Map qryInfo(int userid) throws Exception {
        User user1 = userDao.qryById(userid);
        HashMap map = new HashMap();
        map.put("username",user1.getUsername());
        map.put("sex",user1.getSex());
        map.put("telnumber",user1.getTelnumber());
        return map;
    }

    @Override
    public void deleteUserById(int userid) throws Exception {
        userDao.deleteUserById(userid);
    }

    @Override
    public List<User> findAllUser() throws Exception {
        return userDao.findAllUser();
    }

    @Override
    public void updatePassword(String password, int userid) throws Exception {
        userDao.UpdatePwdById(password,userid);
    }
}
